import os

import cv2


class SequenceHandler:
    def __init__(self, path):
        self.path = path
        self.name = os.path.basename(self.path)

    def __call__(self):
        cap = cv2.VideoCapture(self.path)
        if not cap.isOpened():
            raise IOError('Cannot open video file')

        while cap.isOpened():
            ret, frame = cap.read()

            if not ret:
                break

            cv2.imshow(self.name, frame)

            key = cv2.waitKey(30)
            if key & 0xFF == 27:
                break

            if cv2.getWindowProperty(self.name, cv2.WND_PROP_VISIBLE) < 1:
                break

        cap.release()

        cv2.destroyAllWindows()


if __name__ == "__main__":
    seq = SequenceHandler("../materials/skibidi.mp4")
    seq()
